package com.tedu.egmvcboot.controller;

import com.tedu.egmvcboot.mapper.UserMapper;
import com.tedu.egmvcboot.pojo.dto.UserLoginDTO;
import com.tedu.egmvcboot.pojo.entity.User;
import com.tedu.egmvcboot.pojo.vo.UserVO;
import com.tedu.egmvcboot.result.JsonResult;
import com.tedu.egmvcboot.result.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/v1/users")
@Slf4j
@Api(tags = "用户模块")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public JsonResult login(@RequestBody @Validated UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session){
        //log.info("UserController.login()方法执行了...");
        //log.debug("userLoginDTO:"+userLoginDTO);
        UserVO userVO = userMapper.selectUser(userLoginDTO.getUsername(),userLoginDTO.getPassword());
        if (userVO!=null){//查询到该用户,且密码正确
                session.setAttribute("userVO",userVO);
                //log.debug("userVO:"+userVO);
                return JsonResult.success();
        }else {//该用户不存在或者密码不正确
            return new JsonResult(Status.USERNAME_PASSWORD_ERROR);
        }
    }
}
